//
//  PhotosView.m
//  PigMarket
//
//  Created by mac on 2019/6/13.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "PhotosView.h"

@implementation PhotosView

#pragma mark - init method
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)configPhotosWithImgsArr:(NSArray *)imgsArr {
    // 添加子控件
    int count = (int)imgsArr.count;
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = 0;
    CGFloat h = 0;

    if (count == 1) {
        w = SCREEN_WIDTH - 54;
        h = 212 * [JTool getScreenWidthScale];
    } else {
        w = (SCREEN_WIDTH - 60 ) / 3;
        h = (SCREEN_WIDTH - 60 ) / 3;
        if (count == 6) {
            count = 6;
        }
    }
    
    for (int i = 0; i < count; i++) {
        x = i % 3 * (w + 3);
        y = i / 3 * (h + 3);
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];
        imgView.clipsToBounds = YES;
        imgView.contentMode = UIViewContentModeCenter;
        [imgView sd_setImageWithURL:[NSURL URLWithString:imgsArr[i]] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];
        imgView.userInteractionEnabled = YES;
        [self addSubview:imgView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTapped:)];
        [imgView addGestureRecognizer:tap];
        imgView.tag = 100 + i;
    }
}
- (void)photoTapped:(UITapGestureRecognizer *)tap {
    
    NSInteger index = [tap view].tag;
    if (self.PhotoTappBlock) {
        self.PhotoTappBlock(index);
    }
}

#pragma mark - getter and setter

@end
